<?php 
class ObserverView {
	
	const UPDATE_POST = "update";
	const SUBMENUID_URLPARAMETER = "submenu";
	const CATEGORYID_URLPARAMETER = "categoryid";
	const POST_URLPARAMETER = "post";	

	const EDIT_POST_TITLE = "Redigera post";
	const DELETE_POST_TITLE = "Ta bort inlägg";
	const POST_HEADER = "header";
	const POST_CONTENT = "content";
	const POSTID = "postId";
	const PARENTID = "parentId";
	
	const DELETE_POST = "deletepost";
	
	const CATEGORYID = "categoryId";
	const CATEGORYNAME = "categoryName";
	
	const SUBCATEGORYID = "subcategoryId";
	const SUBCATEGORYNAME = "subcategoryName";
	
	const SHORTDESCRIPTIONNAME = "shortDescriptionName";
	
	public function IsCategorySet(){
		if(isset($_GET[self::CATEGORYID_URLPARAMETER])){
			return true;
		}else{
			return false;
		} 
	}
	
	public function TriedToDeletePost (){
		if(isset($_POST[self::DELETE_POST])){
			return true;
		}else{
			return false;
		}
	}
	
	public function GetCurrentPostId (){
		if(isset($_GET[self::POST_URLPARAMETER])){
			return $_GET[self::POST_URLPARAMETER];
		}else{
			return false;
		}
	}
	
	public function TriedToUpdatePost (){
		if(isset($_POST[self::UPDATE_POST])){
			return true;
		}else{
			return false;
		}
	}
	
	public function GetHeader (){
		return $_POST[self::POST_HEADER];
	}
	
	public function GetContent(){
		return $_POST[self::POST_CONTENT];
	}
	
	public function GetCategoryId (){
		if(isset($_GET[self::CATEGORYID_URLPARAMETER])){
			return $_GET[self::CATEGORYID_URLPARAMETER];
		}else{
			return false;
		}
	}
	
	public function MakeStartPage ($a_categorysArray) {
		$ret = "";
		
		$ret .= "<div class='startPageTxt'>	<h2>Student i Kalmar</h2>
				
<p>Student i Kalmar är för dig som är ny student i Kalmar. Ett enklare sätt att hitta det man behöver veta de första veckorna för när väl chocken över lnu.se grafiska profil har lagt sig (den är gul!) är sidan fortfarande stor och svår att hitta på. Därför finns Student i Kalmar som bara visar det göttaste av det göttaste; Var du ska bo, var universitetet ligger och hur man byter cykeldäck!</p>
				</div>";
				
		$ret .= $this->RenderIconsStartPage($a_categorysArray);
		return $ret;
	}
	
	//The four images on the startpage. They relies on that the categoryIds in database don't change... Not so good. 
	public function RenderIconsStartPage($a_categoryArray){
		$ret ="";
		$i =1;
		foreach($a_categoryArray as $category){
			if($category[self::PARENTID]==0 && $i<5){
				$ret .= "<div class='col'>
				<a href='?".self::CATEGORYID_URLPARAMETER."=".$category[self::CATEGORYID]."'><img src='img/".$category[self::CATEGORYNAME].".jpg'></a>
				<h2><a href='?".self::CATEGORYID_URLPARAMETER."=".$category[self::CATEGORYID]."' class='bg".$i."'>".$category[self::CATEGORYNAME]."</a></h2>
				<p>".$category[self::SHORTDESCRIPTIONNAME]."</p>
				</div>";
				$i++;
			}
		}
		
		return $ret;
	}
	
	//Builds topmenu
	public function BuildMenu ($a_menuArray){
		$ret = "\n <ul id='sddm'> \n";
		
		foreach($a_menuArray as $value){
			if ($value[self::PARENTID]== null){
					$ret .= "	<li><a href='".$_SERVER['SCRIPT_NAME']."?".self::CATEGORYID_URLPARAMETER."=".$value[self::CATEGORYID]."' 
					title='".$value[self::CATEGORYNAME]."' onmouseover='mopen(\"m".$value[self::CATEGORYID]."\");'
					onmouseout='mclosetime()'>";
					$ret .= $value[self::CATEGORYNAME];//mopen(\"m".$value[ObserverController::CATEGORYID]."\")
					$ret .= '</a>';
					$ret .= $this->GetChilds($a_menuArray, $value[self::CATEGORYID]);//loop through childs
					$ret .= '	</li>' . "\n";
			}else {
				//annars så  die ( sprintf ( 'menu nr %s must be an array' ) );
			}
		}
		
		$ret .= '</ul>'."\n";
		return $ret;
	}	
	
	//Makes submenu
	public function GetChilds($a_menuArray, $a_parentId){
		$hasSubcats = FALSE;
		$ret = "\n"."		<div id = 'm".$a_parentId."' onmouseover='mcancelclosetime()' onmouseout='mclosetime()'><ul>" . "\n";
		//<div class='subcatsdiv' id='".$a_parentId."'  onmouseover='".mcancelclosetime()."' 
          //  onmouseout='".mclosetime().">
		foreach($a_menuArray as $value){
			if ( $value[self::PARENTID] == $a_parentId){
				$hasSubcats = TRUE;
				$ret .= "			<li><a href='".$_SERVER['SCRIPT_NAME']."
				?".ObserverView::CATEGORYID_URLPARAMETER."=".$a_parentId."
				&".self::SUBMENUID_URLPARAMETER."=".$value[self::CATEGORYID]."' 
									title='".$value[self::CATEGORYNAME]."'
				 >";
				$ret .= $value[self::CATEGORYNAME];
				$ret .= '</a>';
				$ret .= '			</li>' . "\n";
			}
		}
		$ret .= '		</ul>'."\n</div>";
		return ($hasSubcats) ? $ret : false;
	}
	
	public function RenderPosts ($a_allPosts, $a_userIsLoggedIn){
		$ret = "";
		foreach($a_allPosts as $post){
			if($post->m_parentId == $_GET[self::CATEGORYID_URLPARAMETER] && 
										isset($_GET[self::SUBMENUID_URLPARAMETER]) && 
										$_GET[self::SUBMENUID_URLPARAMETER] == $post->m_categoryId){
											
				$ret .= "<div class='post'><div class='postHeader'><h2>".$post->m_postHeader."</div></h2>"
						.$post->m_postContent."";
				
				//Editbutton will only be visible if you're logged in. 
				if($a_userIsLoggedIn){
					$ret .= $this->EditButton($post);
				}
				$ret .= "</div></p>";
			}elseif(isset($_GET[self::CATEGORYID_URLPARAMETER]) && !isset($_GET[self::SUBMENUID_URLPARAMETER])
				&& $post->m_categoryId == $_GET[self::CATEGORYID_URLPARAMETER] 
				||$post->m_parentId == $_GET[self::CATEGORYID_URLPARAMETER] && !isset($_GET[self::SUBMENUID_URLPARAMETER])){
				$ret .= "<div class='post'><div class='postHeader'><h1>".$post->m_postHeader."</h1></div>"
						.$post->m_postContent."";

				//Editbutton will only be visible if you're logged in. 
				if($a_userIsLoggedIn){
					$ret .= $this->EditButton($post);
				}
				$ret .= "</div></p>";
			}
		}
		return $ret;
	}
	
	//Makes xhtml edit post button
	public function EditButton ($a_post){
		return  "<p><a href='?".NavigationView::CONTROLLER."=".NavigationView::UPDATE_POST."&".
					self::CATEGORYID_URLPARAMETER."=".$a_post->m_categoryId."&".self::POST_URLPARAMETER."=".$a_post->m_postId."' 
					title='".self::EDIT_POST_TITLE."'>".self::EDIT_POST_TITLE."</a>";
	}
	
	//Update post form prefilled. 
	public function AddImageEditPost($a_header, $a_postId, $a_content, $a_fileUploadInputs){
		return "<h1>Redigera post</h1>
		<form method='post' name='updatePost' action='' enctype = 'multipart/form-data' >
			<h2>Ange rubrik</h2><input type='text' name='".self::POST_HEADER."' text =''value ='".$a_header."' /><br />
			<textarea name='".self::POST_CONTENT."' id='content' class='mceEditor' rows='25' cols='100' tabindex='2'>".$a_content."</textarea>
			<input type='hidden' name= '".self::POST_URLPARAMETER."' id='' value='".$a_postId."'/>
			<input type='submit' name='".self::UPDATE_POST."' value='Uppdatera'/>
			$a_fileUploadInputs
			
		</form>
		<form method='post' name='deletePost' action=''>
			<input type='hidden' name= '' id='".self::POST_URLPARAMETER."' value='".$a_postId."'/>
			<input type='submit' name='".self::DELETE_POST."' onclick='showconfirmbox('Vill du verkligen ta bort mig?')' value='Ta bort post'/>
		</form>";
	}
}
